// pages/searcg/index.js
import { $http } from '../../request/index-api'
import regeneratorRuntime from '../../lib/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    values: '',
    listData: [],
    isShowClearBtn:true
  },
  timerId: -1,

  onShow () {
    console.log(typeof this.data.values.trim());
    if(this.data.values.trim()==''){
      this.setData({
        isShowClearBtn:false
      })
    }
  },
  changeInputValue (e) {
    this.getSearchInfo()
    if(this.data.values.trim()==''){
      this.setData({
        isShowClearBtn:false
      })
    }
    this.setData({
      isShowClearBtn:true
    })
  },
  async getSearchInfo () {
    //防抖动技术
    clearTimeout(this.timerId)
    this.timerId = setTimeout(async() => {
      const res = await $http({ url: '/goods/qsearch', data: { query: this.data.values } })
      this.setData({
        listData: res
      })
    }, 1000);
  },
  clearInpupt(){
    this.setData({
      values:'',
      listData:'',
      isShowClearBtn:false
    })
  }
})